package com.bytedance.danmaku.render.engine.demo.base

import android.net.Uri
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.bytedance.danmaku.render.engine.render.draw.text.TextData
import com.bytedance.danmaku.render.engine.utils.LAYER_TYPE_SCROLL
import com.bytedance.danmaku.render.engine.demo.R
import com.bytedance.danmaku.render.engine.BaseDanmakuData
import com.bytedance.danmaku.render.engine.demo.view.CustomVideoView
import com.bytedance.danmaku.render.engine.demo.view.DanmakuRecyclerView

/**
 * Created by dss886 on 2021/07/01.
 */
abstract class BaseDemoActivity : AppCompatActivity() {

    protected lateinit var mVideoView: CustomVideoView
    protected lateinit var mDanmakuData: List<BaseDanmakuData>

    abstract fun getLayoutId(): Int

    abstract fun init()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getLayoutId())
        supportActionBar?.apply {
            setDisplayHomeAsUpEnabled(true)
            setDisplayShowHomeEnabled(true)
        }

        mDanmakuData = buildDanmakuData()

        findViewById<CustomVideoView>(R.id.video_view)?.apply {
            mVideoView = this
            setVideoURI(Uri.parse("android.resource://" + packageName + "/" + R.raw.video))
        }

        findViewById<DanmakuRecyclerView>(R.id.danmaku_recycler_view)?.apply {
            data = mDanmakuData
        }

        init()
        mVideoView.start()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onDestroy() {
        mVideoView.stopPlayback()
        super.onDestroy()
    }

    protected open fun buildDanmakuData(): List<BaseDanmakuData> {
        val contents = resources.getStringArray(R.array.danmaku_text_contents)
        val list = mutableListOf<TextData>()
<<<<<<< HEAD
//        list.add(TextData().apply {
//            text = "测试地方加快速度"
//            showAtTime = 5350
//            layerType = LAYER_TYPE_SCROLL
////                showAtTime = i * 600L +  (0..300L).random()
//
//        })
        for (i in 0..3)
        {
            list.add(TextData().apply {
                text = "10测试地风扇电机快捷方式方加快速度${i}"
                showAtTime = 1000L
                layerType = LAYER_TYPE_SCROLL
=======
        for (i in 0..20) {
            list.add(TextData().apply {
                id="$i"
                content="弹幕内容附件是独立空间法律是浪费点卡====$i"
                text = "弹幕内容附件是独立空间法律是浪费点卡====$i"
                layerType = LAYER_TYPE_SCROLL
//                showAtTime = i * 600L +  (0..300L).random()
                showAtTime =  i*1000L
>>>>>>> 6b8cf5e9a9a93ce922a307dd5456443c78310813
            })
        }
        for (i in 20..50) {
//            if(i%2==0)
//            {
                list.add(TextData().apply {
                    text = "23测试地风扇电机傅雷家书放得开了方加快速度${i}"
                    showAtTime = i * 1000L
                    layerType = LAYER_TYPE_SCROLL
//                showAtTime = i * 600L +  (0..300L).random()

                })
//                list.add(TextData().apply {
//                    text = "测试文字2都发了${i}"
//                    showAtTime = i * 1000L
//                    layerType = LAYER_TYPE_SCROLL
////                showAtTime = i * 600L +  (0..300L).random()
//
//                })
//                list.add(TextData().apply {
//                    text = "测试文字2都发了${i}"
//                    showAtTime = i * 1000L
//                    layerType = LAYER_TYPE_SCROLL
////                showAtTime = i * 600L +  (0..300L).random()
//
//                })
//            }
//            else
//            {
//                list.add(TextData().apply {
//                    text = "文字水电费加快速度封建势力反垃圾${i}"
//                    showAtTime = i * 1000L
//                    layerType = LAYER_TYPE_SCROLL
////                showAtTime = i * 600L +  (0..300L).random()
//
//                })
//            }

        }
        return list
    }

}